#include "StdAfx.h"
#include "CREWaterVolume.h"


CREWaterVolume::CREWaterVolume()
: CRendElementBase()
, m_pParams( 0 )
, m_pOceanParams( 0 )
, m_drawWaterSurface( false )
, m_drawFastPath( false )
, m_pRenderMesh(NULL)

{
	mfSetType( eDATA_WaterVolume );
	mfUpdateFlags( FCEF_TRANSFORM );
}


CREWaterVolume::~CREWaterVolume()
{
	if (m_pRenderMesh)
	{
		m_pRenderMesh->Release();
		m_pRenderMesh = NULL;
	}
}


void CREWaterVolume::mfGetPlane(Plane& pl)
{
  pl = m_pParams->m_fogPlane;
  pl.d = -pl.d;
}

void CREWaterVolume::mfCenter(Vec3& vCenter, CRenderObject *pObj)
{
  vCenter = m_pParams->m_center;
  if (pObj)
    vCenter += pObj->GetTranslation();
}

void CREWaterVolume::mfPrepare(bool bCheckOverflow)
{
	if (bCheckOverflow)
		gRenDev->FX_CheckOverflow( 0, 0, this );
	gRenDev->m_RP.m_pRE = this;
	gRenDev->m_RP.m_RendNumIndices = 0;
	gRenDev->m_RP.m_RendNumVerts = 0;

	if( !m_pRenderMesh && !gRenDev->IsEditorMode() && m_pParams->m_useStaticMesh )
	{
		m_pRenderMesh = gEnv->pRenderer->CreateRenderMeshInitialized(
			(void *)m_pParams->m_pVertices,
			(int)m_pParams->m_numVertices,
			eVF_P3F_C4B_T2F,
			const_cast<uint16 *>(m_pParams->m_pIndices),
			(int)m_pParams->m_numIndices,
			prtTriangleList,
			"WaterVolumeObject",
			"WaterVolumeObject"
			);
	}

}


float CREWaterVolume::mfDistanceToCameraSquared( Matrix34& matInst )
{
	assert( m_pParams );
	if( !m_pParams )
		return 0;

	CRenderer* rd( gRenDev );
	Vec3 delta( rd->GetRCamera().Orig - m_pParams->m_center );
	return delta.GetLengthSquared();
}
